package com.cuit.bysj.service;

import java.util.ArrayList;
import java.util.List;

import com.cuit.bysj.dao.AppraiseTcherDao;
import com.cuit.bysj.entities.Appraise;
import com.cuit.bysj.entities.Item;
import com.cuit.bysj.entities.Select;
import com.cuit.bysj.entities.Student;
import com.cuit.bysj.entities.User;

public class AppraiseTcherService {
	private AppraiseTcherDao appraisetcherDao;

	public AppraiseTcherDao getAppraisetcherDao() {
		return appraisetcherDao;
	}

	public void setAppraisetcherDao(AppraiseTcherDao appraisetcherDao) {
		this.appraisetcherDao = appraisetcherDao;
	}

	// 加载学生需要评价的教师列表业务
	@SuppressWarnings("unchecked")
	public List<Appraise> loadlist(Student stu) {
		String hql = "from Appraise a where a.student = " + stu.getId() + " and a.score = 0";

		List<Appraise> list = appraisetcherDao.loadlist(hql);

		if (list.size() > 0)
			return list;

		return null;
	}

	// 加载评教条目
	@SuppressWarnings("unchecked")
	public List<Item> initItem() {
		String hql = "from Item";

		List<Item> list = appraisetcherDao.loadlist(hql);

		return list;
	}

	// android端通过请求的id查询实体的业务
	public User findUser(Integer id) {
		User user = appraisetcherDao.getByID(id);

		return user;
	}

	// 保存评教表
	public void saveform(String id, String content, String score, String str) {
		Appraise appraise = appraisetcherDao.getAppraiseByID(Integer.parseInt(id));

		Select s = new Select();
		if (appraise.getSelection() != null) {
			appraise.getSelection().setSelectJson(str);
		} else {
			s.setSelectJson(str);
			appraisetcherDao.addSelect(s);
			appraise.setSelection(s);
		}

		appraise.setContent(content);
		appraise.setScore(Double.parseDouble(score));

		appraisetcherDao.updateform(appraise);
	}

	// 判断评教是否开启，返回boolean类型
	public boolean isOpen() {
		return appraisetcherDao.queryState(1).getState();
	}

	public Appraise findAppraiseTable(Integer id) {
		return appraisetcherDao.getAppraise(id);
	}

	public void updateAppraise(Appraise a) {
		appraisetcherDao.updateform(a);
	}
}
